package com.fzkj.picconvert;

import sun.misc.BASE64Encoder;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class PicConvert {

    // 图片转换为base64

    public static void main(String[] args) {
        String imgStr = "";
        try {

            File file = new File("E:/27a01.jpg");
            FileInputStream fis = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            int offset = 0;
            int numRead = 0;
            while (offset < buffer.length && (numRead = fis.read(buffer, offset, buffer.length - offset)) >= 0) {
                offset += numRead;
            }

            if (offset != buffer.length) {
                throw new IOException("Could not completely read file "
                        + file.getName());
            }
            fis.close();
            BASE64Encoder encoder = new BASE64Encoder();
            imgStr = encoder.encode(buffer);
            System.out.println("转换之后的："+imgStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
